home *** CD-ROM | disk | FTP | other *** search
- Path: gate.net!pslfl2-51
- From: bhutto@gate.net (William Hutto)
- Newsgroups: comp.lang.c
- Subject: Re: array of pointers to a tree structure ? ( right post, the other one has a miss * )
- Date: 10 Jan 1996 22:40:20 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4d1f8k$ni2@news.gate.net>
- References: <4d06ge$9lc@hermes.fundp.ac.be>
- NNTP-Posting-Host: pslfl2-25.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4d06ge$9lc@hermes.fundp.ac.be>,
- Francisco Melo Ledermann <fmelo@biq.fundp.ac.be> wrote:
- >Hi Colleagues, I have been learning C language just from one month ago
- >and I don't know too much about this language. I have a problem with the
- >assignment of pointers in an array of pointers with a specifical defined
- >structure. For example:
- >
- >
- >/* BOXES STRUCTURE FOR THE CONSTRUCTION OF A TREE */
- >
- >struct boxes {
- > int number;
- > struct boxes *pointer_1;
- > struct boxes *pointer_2;
- > struct boxes *pointer_3;
- > }
- ^
- Semicolon goes here.
-
- >
- >/* ARRAY OF POINTERS TO BOXES STRUCTURE FOR MANAGE */
- >/* SOME BRANCHES OF THE TREE */
- >
- >struct boxes *array_pointers [15];
-
- Now this makes a big difference from your first post (which I answered but
- didn't send). You've defined and array of 15 pointers of type _struct boxes_
- which point to NULL if statically defined or to *God knows where* if they're
- automatic.
-
- >
- >
- >Supose that I have the array of pointers already defined, with every
- >pointer pointing to a boxes structure and every boxes structure contains
- >only the integer number defined. In this moment I would like to define
- >the three pointers in every box in the way of these pointers point to
- >another element in the array. I have tried statements like this with out
- >success:
- >
- >
- >(*(array_pointers + 1)).pointer_1 = (array_pointers + 0);
- >
- >or
- >
- >(array_pointers[1]).pointer_1 = &array_pointers[0];
- >
-
- You're trying to access members of a structure that does not physically exist.
- All pointers should be properly initialized, as per type, before using them.
- In this case, you could use malloc() (#include <stdlib.h>). Also, you would
- use the indirect member selector (->) to access members of the structures.
-
- func()
- {
- int i;
- struct boxes *array_pointers[15];
-
- for(i=0;i<15;i++) {
- if((array_pointers[i]=malloc(sizeof(struct boxes)))==NULL) {
- /*ERROR: Insufficient memory*/
- }
- }
- array_pointers[1]->pointer_1 = array_pointers[0];
-
- array_pointers[1]->pointer_1->number=1234;
-
- printf("%d\n",array_pointers[0]->number); /*prints 1234*/
- }
-
- Read the comp.lang.c FAQ which contains several chapters on information you
- need here.
-
- ftp://rtfm.mit.edu/pub/usenet-by-group/comp.lang.c/C-FAQ-list
-
- Bill
-
- "Whatcha got on?...Your mind?"
-